package com.cjy.dao.daoImpl;

import com.cjy.dao.inter.bookDao;
import com.cjy.db.BaseDao;
import com.cjy.pojo.Book;
import com.cjy.pojo.Page;
import com.cjy.pojo.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class bookDaoImpl implements bookDao {
    BaseDao baseDao = new BaseDao();

    @Override
    public boolean login(User user) {
        String sql = "select * from user where uname = ? and password = ?";
        String[] param = new String[]{
                user.getUsername(),
                user.getPassword()
        };
        ResultSet resultSet = baseDao.executeQuery(sql, param);

        try {
            if (resultSet.next()) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int register(User user) {
        String sql = "insert into user (uname, password) values (?, ?)";
        String[] param = new String[]{
                user.getUsername(),
                user.getPassword()
        };

        return baseDao.executeSQL(sql, param);
    }

    @Override
    public int insertBook(Book book) {
        String sql = "insert into tb_book (isbn, name, price, press) values (?, ?, ?, ?)";
        String[] param = new String[]{
                book.getIsbn(),
                book.getName(),
                book.getPrice(),
                book.getPress()
        };

        return baseDao.executeSQL(sql, param);
    }

    @Override
    public List<Book> selectBook() {
        List<Book> list = new ArrayList<Book>();
        String sql = "select * from tb_book";
        ResultSet resultSet = baseDao.executeQuery(sql);

        try {
            while (resultSet.next()) {
                Book book = new Book();
                book.setId(resultSet.getInt("id"));
                book.setIsbn(resultSet.getString("isbn"));
                book.setName(resultSet.getString("name"));
                book.setPrice(resultSet.getString("price"));
                book.setPress(resultSet.getString("press"));

                list.add(book);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public List<Book> selectBook(Page page) {
        List<Book> list = new ArrayList<Book>();
        String sql = "select * from tb_book limit ?, ?";
        Integer[] param = new Integer[]{
                page.getCurrentNum(),
                page.getPageSize()
        };
        ResultSet resultSet = baseDao.executeQuery(sql, param);

        try {
            while (resultSet.next()) {
                Book book = new Book();
                book.setId(resultSet.getInt("id"));
                book.setIsbn(resultSet.getString("isbn"));
                book.setName(resultSet.getString("name"));
                book.setPrice(resultSet.getString("price"));
                book.setPress(resultSet.getString("press"));

                list.add(book);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public List<Book> selectBook(String type, String input) {
        List<Book> list = new ArrayList<Book>();
        String sql = "select * from tb_book where " + type + " like '%" + input + "%'";

        ResultSet resultSet = baseDao.executeQuery(sql);
        try {
            while (resultSet.next()) {
                Book book = new Book();
                book.setId(resultSet.getInt("id"));
                book.setIsbn(resultSet.getString("isbn"));
                book.setName(resultSet.getString("name"));
                book.setPrice(resultSet.getString("price"));
                book.setPress(resultSet.getString("press"));
                list.add(book);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public List<Book> selectBook(String type, String input, Page page) {
        List<Book> list = new ArrayList<Book>();
        String sql = "select * from tb_book where " + type + " like '%" + input + "%' limit ?, ?";
        Integer[] param = new Integer[]{
                page.getCurrentNum(),
                page.getPageSize()
        };

        ResultSet resultSet = baseDao.executeQuery(sql, param);
        try {
            while (resultSet.next()) {
                Book book = new Book();
                book.setId(resultSet.getInt("id"));
                book.setIsbn(resultSet.getString("isbn"));
                book.setName(resultSet.getString("name"));
                book.setPrice(resultSet.getString("price"));
                book.setPress(resultSet.getString("press"));
                list.add(book);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public Book selectBook(Integer id) {
        String sql = "select * from tb_book where id = ?";
        String[] param = new String[]{
                id.toString()
        };
        ResultSet resultSet = baseDao.executeQuery(sql, param);

        Book book = new Book();

        try {
            while (resultSet.next()) {
                book.setId(resultSet.getInt("id"));
                book.setIsbn(resultSet.getString("isbn"));
                book.setName(resultSet.getString("name"));
                book.setPrice(resultSet.getString("price"));
                book.setPress(resultSet.getString("press"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return book;
    }

    @Override
    public int deleteBook(Integer id) {
        String sql = "delete from tb_book where id = ?";
        String[] param = new String[]{
                String.valueOf(id)
        };

        return baseDao.executeSQL(sql, param);
    }

    @Override
    public int updateBook(Book book) {
        String sql = "update tb_book set isbn = ?, name = ?, price = ?, press = ? where id = ?";
        String[] param = new String[]{
                book.getIsbn(),
                book.getName(),
                book.getPrice(),
                book.getPress(),
                book.getId().toString()
        };

        return baseDao.executeSQL(sql, param);
    }
}
